<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="上报时间">
        <el-date-picker
          v-model="daterangeReportingDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="班组" prop="team">
        <el-input
          v-model="queryParams.team"
          placeholder="请输入班组"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="制袋月" prop="bagMakingMonth">
        <el-input
          v-model="queryParams.bagMakingMonth"
          placeholder="请输入制袋月"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="制袋日" prop="bagMakingDay">
        <el-input
          v-model="queryParams.bagMakingDay"
          placeholder="请输入制袋日"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="制袋时间" prop="bagMakingTime">
        <el-date-picker clearable
          v-model="queryParams.bagMakingTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择制袋时间">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="制袋人员" prop="bagMakingPersonnel">
        <el-input
          v-model="queryParams.bagMakingPersonnel"
          placeholder="请输入制袋人员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="工单ID" prop="workorderId">
        <el-input
          v-model="queryParams.workorderId"
          placeholder="请输入工单ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="工单号" prop="workorderId">
        <el-input
          v-model="queryParams.workorderId"
          placeholder="请输入工单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="工单名称" prop="workorderName">
        <el-input
          v-model="queryParams.workorderName"
          placeholder="请输入工单名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品ID" prop="itemId">
        <el-input
          v-model="queryParams.itemId"
          placeholder="请输入产品ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="产品编号" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入产品编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入产品名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['fb:inspection:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['fb:inspection:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['fb:inspection:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-upload2" size="mini" @click="handleImport"
                   v-hasPermi="['mes:fb:inspection:import']">导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
                 :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
                 :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!-- <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
          </div> -->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                   @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
    <el-table v-loading="loading" :data="inspectionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="记录ID" fixed  align="center" prop="recordId" /> -->
      <el-table-column label="年" fixed  align="center" prop="year" />
      <el-table-column label="月" fixed width="60"  align="center" prop="month" />
      <el-table-column label="日" fixed  width="60" align="center" prop="day" />
      <!-- <el-table-column label="上报时间" align="center" prop="reportingDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reportingDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="班组" fixed  align="center" prop="team" />
      <el-table-column label="制袋月" fixed  align="center" prop="bagMakingMonth" width="60">
      </el-table-column>
      <el-table-column label="制袋日" fixed  align="center" prop="bagMakingDay" width="60">
      </el-table-column>
      <el-table-column label="制袋人员" fixed  align="center" prop="bagMakingPersonnel" />
      <!-- <el-table-column label="工单ID" align="center" prop="workorderId" /> -->
      <el-table-column label="工单号" align="center" fixed prop="workorderId" />
      <!-- <el-table-column label="工单名称" align="center" prop="workorderName" /> -->
      <!-- <el-table-column label="产品ID" align="center" prop="itemId" /> -->
      <el-table-column label="产品编号" fixed  width="120" align="center" prop="itemCode" />
      <el-table-column label="产品名" fixed   width="120" align="center" prop="itemName" />
      <el-table-column label="返修品"  align="center" prop="repairedProducts" >
        <el-table-column label="单绳" align="center" prop="singleRope" />
        <el-table-column label="勾底" align="center" prop="tickBottom" />
        <el-table-column label="袋面脏" align="center" prop="dirtyBagSurface" />
        <el-table-column label="褶皱" align="center" prop="fold" />
        <el-table-column label="合计" align="center" prop="total" />
      </el-table-column>
      <el-table-column label="报废品"  align="center" prop="scrapReporting" >
        <el-table-column label="印废" align="center" prop="printingWaste" />
        <el-table-column label="制废" align="center" prop="wasteProduction" />
      </el-table-column>
      <el-table-column label="生产总数(打包)" align="center" prop="productionTotal">
        <el-table-column label="纸皱" align="center" prop="paperWrinkling" />
        <el-table-column label="次品" align="center" prop="defectiveProducts" />
        <el-table-column label="合格品" align="center" prop="qualifiedProducts" />
      </el-table-column>
      <el-table-column label="不良统计" align="center" prop="negativeStatistics" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['fb:inspection:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['fb:inspection:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改成品检验对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1100px" append-to-body>
      <el-form ref="form" :model="form" inline :rules="rules" label-width="120px">
        <!-- <el-form-item label="上报时间" prop="reportingDate">
          <el-date-picker clearable
            v-model="form.reportingDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择上报时间">
          </el-date-picker>
        </el-form-item> -->
        <el-form-item label="年" prop="year">
          <el-input v-model="form.year" type="number" placeholder="请输入年" />
        </el-form-item>
        <el-form-item label="月" prop="month">
          <el-input v-model="form.month" type="number" placeholder="请输入月" />
        </el-form-item>
        <el-form-item label="日" prop="day">
          <el-input v-model="form.day" type="number" placeholder="请输入日" />
        </el-form-item>
        <el-form-item label="班组" prop="team">
          <el-input v-model="form.team" placeholder="请输入班组" />
        </el-form-item>
        <el-form-item label="制袋月" prop="bagMakingMonth">
          <el-input v-model="form.bagMakingMonth" type="number" placeholder="请输入制袋月" />
        </el-form-item>
        <el-form-item label="制袋日" prop="bagMakingDay">
          <el-input v-model="form.bagMakingDay" type="number" placeholder="请输入制袋日" />
        </el-form-item>
        <!-- <el-form-item label="制袋时间" prop="bagMakingTime">
          <el-date-picker clearable
            v-model="form.bagMakingTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择制袋时间">
          </el-date-picker>
        </el-form-item> -->
        <el-form-item label="制袋人员" prop="bagMakingPersonnel">
          <el-input v-model="form.bagMakingPersonnel" placeholder="请输入制袋人员" />
        </el-form-item>
        <!-- <el-form-item label="工单ID" prop="workorderId">
          <el-input v-model="form.workorderId" placeholder="请输入工单ID" />
        </el-form-item> -->
        <el-form-item label="工单号" prop="workorderId">
          <el-input v-model="form.workorderId" type="number" placeholder="请输入工单号" />
        </el-form-item>
        <!-- <el-form-item label="工单名称" prop="workorderName">
          <el-input v-model="form.workorderName" placeholder="请输入工单名称" />
        </el-form-item> -->
        <!-- <el-form-item label="产品ID" prop="itemId">
          <el-input v-model="form.itemId" placeholder="请输入产品ID" />
        </el-form-item> -->
        <el-form-item label="产品编号" prop="itemCode">
          <el-input v-model="form.itemCode" placeholder="请输入产品编号" />
        </el-form-item>
        <el-form-item label="产品名" prop="itemName">
          <el-input v-model="form.itemName" placeholder="请输入产品名" />
        </el-form-item>
        <el-form-item label="单绳" prop="singleRope">
          <el-input v-model="form.singleRope" type="number" placeholder="请输入单绳" />
        </el-form-item>
        <el-form-item label="勾底" prop="tickBottom">
          <el-input v-model="form.tickBottom" type="number" placeholder="请输入勾底" />
        </el-form-item>
        <el-form-item label="袋面脏" prop="dirtyBagSurface">
          <el-input v-model="form.dirtyBagSurface" type="number" placeholder="请输入袋面脏" />
        </el-form-item>
        <el-form-item label="褶皱" prop="fold">
          <el-input v-model="form.fold" type="number" placeholder="请输入褶皱" />
        </el-form-item>
        <el-form-item label="合计" prop="total">
          <el-input v-model="form.total" type="number" placeholder="请输入合计" />
        </el-form-item>
        <!-- <el-form-item label="返修品 - 数组" prop="repairedProducts">
          <el-input v-model="form.repairedProducts" type="textarea" placeholder="请输入内容" />
        </el-form-item> -->
        <!-- <el-form-item label="报废品-数组" prop="scrapReporting">
          <el-input v-model="form.scrapReporting" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="生产总数(打包)" prop="productionTotal">
          <el-input v-model="form.productionTotal" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="数据行状态(0:正常/1:删除)" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入数据行状态(0:正常/1:删除)" />
        </el-form-item> -->
        <el-form-item label="印废" prop="printingWaste">
          <el-input v-model="form.printingWaste" type="number" placeholder="请输入印废" />
        </el-form-item>
        <el-form-item label="制废" prop="wasteProduction">
          <el-input v-model="form.wasteProduction" type="number" placeholder="请输入制废" />
        </el-form-item>
        <el-form-item label="纸皱" prop="paperWrinkling">
          <el-input v-model="form.paperWrinkling" type="number" placeholder="请输入纸皱" />
        </el-form-item>
        <el-form-item label="次品" prop="defectiveProducts">
          <el-input v-model="form.defectiveProducts" type="number" placeholder="请输入次品" />
        </el-form-item>
        <el-form-item label="合格品" prop="qualifiedProducts">
          <el-input v-model="form.qualifiedProducts" type="number" placeholder="请输入合格品" />
        </el-form-item>
        <el-form-item label="不良统计" prop="negativeStatistics">
          <el-input v-model="form.negativeStatistics" type="number" placeholder="请输入不良统计" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInspection, getInspection, delInspection, addInspection, updateInspection } from "@/api/mes/fb/inspection";
import {getToken} from "@/utils/auth";
import {parseTime} from "@/utils/ruoyi";

export default {
  name: "Inspection",
  data() {
    return {
      daterangeReportingDate:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 成品检验表格数据
      inspectionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        bagMakingMonth:null,
        bagMakingDay:null,
        pageNum: 1,
        pageSize: 10,
        reportingDate: null,        team: null,        bagMakingTime: null,        bagMakingPersonnel: null,        workorderId: null,        workorderCode: null,        workorderName: null,        itemId: null,        itemCode: null,        itemName: null,        repairedProducts: null,        scrapReporting: null,        productionTotal: null,      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        year: [
          { required: true, message: "年不能为空", trigger: "blur" }
        ],     
        month: [
          { required: true, message: "月不能为空", trigger: "blur" }
        ],     
        day: [
          { required: true, message: "日不能为空", trigger: "blur" }
        ],  
      },
      // 导入参数
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: {
          Authorization: "Bearer " + getToken()
        },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/mes/fb/inspection/importData"
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询成品检验列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeReportingDate && '' != this.daterangeReportingDate) {
        this.queryParams.params["beginReportingDate"] = this.daterangeReportingDate[0];
        this.queryParams.params["endReportingDate"] = this.daterangeReportingDate[1];
      }
      listInspection(this.queryParams).then(response => {
        this.inspectionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "报表导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      // this.download('/mes/fb/flow/importTemplate', {}, `fbReel_template_${new Date().getTime()}.xlsx`)
      this.download('/mes/fb/inspection/importTemplate', {}, `成品检验报表模板.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        recordId: null,negativeStatistics:null,printingWaste:null,wasteProduction:null,paperWrinkling:null,defectiveProducts:null,qualifiedProducts:null,singleRope:null,tickBottom:null,dirtyBagSurface:null,fold:null,total:null,     reportingDate: null,  year: null, month: null, day: null,           team: null,        bagMakingTime: null,        bagMakingPersonnel: null,        workorderId: null,        workorderCode: null,        workorderName: null,        itemId: null,        itemCode: null,        itemName: null,        repairedProducts: null,        scrapReporting: null,        productionTotal: null,        createBy: null,        createTime: null,        updateBy: null,        updateTime: null,        delFlag: null      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeReportingDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.recordId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加成品检验";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const recordId = row.recordId || this.ids
      getInspection(recordId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改成品检验";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.recordId != null) {
            updateInspection(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInspection(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const recordIds = row.recordId || this.ids;
      this.$modal.confirm('是否确认删除成品检验编号为"' + recordIds + '"的数据项？').then(function() {
        return delInspection(recordIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.beforeExportFn();
      this.download('mes/fb/inspection/export', {
        ...this.queryParams
      }, `成品检验日报_${parseTime(new Date().getTime(), '{y}-{m}-{d}-{h}')}.xlsx`)
    }
  }
};
</script>
